package com.game.astd.web;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONException;
import org.json.JSONObject;

import com.game.astd.spirit.model.Pair;
import com.game.astd.web.entity.JsonOpt;

public class WebUtil {
	/**
	 * 判断用户是否已经登录
	 * @param request
	 * @return
	 */
	public static boolean isLogin(HttpServletRequest request) {
		boolean result = false;
		Object obj = request.getSession().getAttribute("_userinfo_");
		if (obj != null) {
			if (obj instanceof HashMap) {
				result = true;
			}
		}
		return result;
	}

	public static List<Map<String, String>> getMyGameAccount(
			HttpServletRequest request) {
		List<Map<String, String>> sMapList = new ArrayList<Map<String, String>>();
		
		List<Map<String, String>> dList = DBUtil.executeQuery("select * from u_game_user where uid=?", new String[] { getLoginUserInfo(request,"uid") });
		
		sMapList.addAll(dList);
		return sMapList;
	}
	
	public static JsonOpt<Boolean> addUser(HttpServletRequest request)
	{
		JsonOpt<Boolean> jsonOpt = new JsonOpt(true);
		jsonOpt.setSuccess(true);
		
		String insertSql = "insert into u_game_user(game_dl,game_area,game_user,game_password,game_nick_name,uid)values(?,?,?,?,?,?)";
		
		String game_dl = request.getParameter("game_dl");
		String game_area = request.getParameter("game_area");
		String game_user = request.getParameter("game_user");
		String game_password = CryptUtil.encode(request.getParameter("game_password"));
		String game_nick_name = request.getParameter("game_nick_name");
		
		DBUtil.executeUpdate(insertSql,new String[]{game_dl,game_area,game_user,game_password,game_nick_name,getLoginUserInfo(request,"uid")});
		return jsonOpt;
	}
	public static JsonOpt<Boolean> saveSetting(HttpServletRequest request)
	{
		String gid = request.getParameter("gid");
		JsonOpt<Boolean> jsonOpt = new JsonOpt(true);
		jsonOpt.setSuccess(true);
		Enumeration<String> paraEnum = request.getParameterNames();
		List<Pair<String,String>> paramList = new ArrayList<Pair<String,String>>();
		StringBuffer deleteSql = new StringBuffer("delete  from u_game_config where gid ="+gid+" and config_name in ('1'");
		StringBuffer insertSql = new StringBuffer("insert into u_game_config(gid,config_name,config_value) values");
		boolean isFirst=false;
		while(paraEnum.hasMoreElements())
		{
			String key = paraEnum.nextElement();
			if(key.startsWith("gameParam_"))
			{
				String pname = key.substring(10);
				String v = request.getParameter(key);
				paramList.add(Pair.makePair(pname, v));
				deleteSql.append(",'").append(pname).append("'");
				
				if(isFirst)
				{	
					insertSql.append(",");
				}
				insertSql.append("(").append(gid).append(",'").append(pname).append("','").append(v).append("')");
				isFirst = true;
			}
			
		}
		deleteSql.append(")");
		//找到所有的键值，删除库中重复的吧
		DBUtil.executeUpdate(deleteSql.toString());
		DBUtil.executeUpdate(insertSql.toString());
		return jsonOpt;
	}
	public static JsonOpt<JSONObject> loadUserConfig(HttpServletRequest request)
	{
		String gid = request.getParameter("gid");
		JSONObject joa = new JSONObject();
		try {
			List<Map<String,String>> resultList = DBUtil.executeQuery("select * from u_game_config where gid=?", new String[]{gid});
			for(Map<String,String> sMap:resultList)
			{
				String k = sMap.get("config_name");
				String v = sMap.get("config_value");
				
				joa.put("gameParam_"+k,v);
			}
		} catch (JSONException e) {
		
		}
		
		JsonOpt<JSONObject> r = new JsonOpt<JSONObject>(joa);
		r.setSuccess(true);
		
		return r;
	}
	public static String getLoginUserInfo(HttpServletRequest request,String property)
	{
		Object obj = request.getSession().getAttribute("_userinfo_");
		if(obj == null)
		{
			return "";
		}
		HashMap<String,String> map =  (HashMap<String,String>)obj;
		return map.containsKey(property)?map.get(property):"";
	}
}
